import { fetchJobs } from '../services/jobs.js'
import { readHtml, writeJson } from '../common/utils.js'
import HtmlQuery from '../common/html_query.js'
import _ from 'lodash'

const splitOrToArray = (input: any) => _.isArray(input) ? input : input.split('<br>')

const ensureArray = (item: any) => _.isArray(item) ? item : [item]


const parseJobDetails = (id: string, html: string) => {
    const query = new HtmlQuery(html)
    query.range('.job-keyword-list').rules('li').concat()
         .rules(['.job-sec-text', 'html', splitOrToArray]).concat()
         .rules('.location-address@text').concat()
         .rules('.job-location-box@data-lat')
    const data = query.build()

    const [skills, description, location, latLong] = data
    const [longitude, latitude] = latLong?.split(',') || []

    return {
        jId: id,
        skills: ensureArray(skills),
        description: ensureArray(description),
        location,
        longitude,
        latitude
    }
}

const setup = async () => {
    const jobs = await fetchJobs()
    const jobDetails = jobs.map((job: any) => {
        const html = readHtml(job.id, 'details')
        return parseJobDetails(job.id, html)
    })

    const json = JSON.stringify(jobDetails, null, 2)
    writeJson('details', json)

    console.log(' => OK')
}

export default setup
